package com.itheima.service.impl;
import java.util.List;

import com.itheima.mapper.StudentMapper;
import com.itheima.mapper.TbBookMapper;
import com.itheima.pojo.TbBookExample;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.pojo.TbBook;
import com.itheima.service.BookService;
import entity.PageResult;
/**
 * 服务实现层
 * @author Administrator
 *
 */
@Service
public class BookServiceImpl implements BookService {

	@Autowired
	private TbBookMapper bookMapper;

	@Autowired
	private StudentMapper studentMapper;

	/**
	 * 查询全部
	 */

	public List<TbBook> findAll() {
		Object o = studentMapper.selectAll();

		return bookMapper.selectByExample(null);
	}

	/**
	 * 按分页查询
	 */

	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		Page<TbBook> page=   (Page<TbBook>) bookMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 增加
	 */

	public void add(TbBook book) {
		bookMapper.insert(book);
	}


	/**
	 * 修改
	 */

	public void update(TbBook book){
		bookMapper.updateByPrimaryKey(book);
	}

	/**
	 * 根据ID获取实体
	 * @param id
	 * @return
	 */

	public TbBook findOne(Long id){
		return bookMapper.selectByPrimaryKey(id+"");
	}

	/**
	 * 批量删除
	 */

	public void delete(Long[] ids) {
		for(Long id:ids){
			bookMapper.deleteByPrimaryKey(id+"");
		}
	}



	public PageResult findPage(TbBook book, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);

		TbBookExample example=new TbBookExample();
		TbBookExample.Criteria criteria = example.createCriteria();

		if(book!=null){
						if(book.getBid()!=null && book.getBid().length()>0){
				criteria.andBidLike("%"+book.getBid()+"%");
			}
			if(book.getTitle()!=null && book.getTitle().length()>0){
				criteria.andTitleLike("%"+book.getTitle()+"%");
			}
			if(book.getAuthor()!=null && book.getAuthor().length()>0){
				criteria.andAuthorLike("%"+book.getAuthor()+"%");
			}

		}

		Page<TbBook> page= (Page<TbBook>)bookMapper.selectByExample(example);
		return new PageResult(page.getTotal(), page.getResult());
	}

}
